﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ManageSystem.Dialog;
using Shared.Model;
using Client;

namespace ManageSystem.Manager
{
    public partial class FormStatistics : UserControl
    {
        private List<NODE> _listNode = null;

        public FormStatistics()
        {
            InitializeComponent();
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            try
            {
                this.listViewStatistics.Items.Clear();

                DateTime startDate = this.dateTimePickerStartDate.Value.Date;
                DateTime start = startDate.AddHours(int.Parse(this.comboBoxStartTime.Text));
                DateTime end = start.AddHours(int.Parse(this.comboBoxLong.Text));

                if (end > DateTime.Now)
                {
                    end = DateTime.Now.AddMinutes(-2);
                }

                long lineId = 0;//产线ID
                long nodeId = 0;//工序ID

                string lineIdStr = this.textBoxLineId.Text;
                string nodeIdStr = this.textBoxNodeId.Text;

                string sqlStr = $"SELECT * FROM View_Process WHERE END_TIME>'{start.ToString("yyyy-MM-dd HH:mm:ss")}' AND END_TIME<'{end.ToString("yyyy-MM-dd HH:mm:ss")}'";

                //PROCESS processW = new PROCESS();
                //processW.SetWhere("END_TIME >'"+start.ToString("yyyy-MM-dd HH:mm:ss")+ "' AND END_TIME<='"+end.ToString("yyyy-MM-dd HH:mm:ss")+"'");//加上时间条件
                //processW.SetWhere();

                if (string.IsNullOrEmpty(lineIdStr) == false)
                {
                    lineId = long.Parse(lineIdStr);
                    //processW.LINE_ID = lineId;
                    sqlStr += $" AND LINE_ID={lineId}";
                }

                if (string.IsNullOrEmpty(nodeIdStr) == false)
                {
                    nodeId = long.Parse(nodeIdStr);
                    //processW.NODE_ID = nodeId;
                    sqlStr += $" AND NODE_ID={nodeId}";
                }

                //var selectProcess = MethodWcf.SelectProcessClient(processW);
                //MessageBox.Show(selectProcess.message);

                var listProcess = Client.MethodWcf.SelectJsonClient<List<PROCESS>>(sqlStr);

                if (listProcess!=null&&listProcess.Count>0)
                {
                    var stationStatistics = (from p in listProcess
                                             group p by new
                                             {
                                                 p.NODE_ID,
                                                 p.LINE_ID,
                                                 p.STATION_ID
                                             } into g
                                             from n in _listNode
                                             where g.Key.NODE_ID==n.ID
                                             orderby n.FLOW_ID, n.SORT,g.Key.LINE_ID
                                             select new
                                             {
                                                 NODE_NAME=n.NAME,
                                                 g.Key.LINE_ID,
                                                 g.Key.STATION_ID,
                                                 ProductNum=g.Count()
                                             }).ToList();

                    if (stationStatistics != null)
                    {
                        for(int i = 0; i < stationStatistics.Count; i++)
                        {
                            ListViewItem item = new ListViewItem
                            {
                                Text = stationStatistics[i].LINE_ID.ToString()
                            };

                            item.SubItems.Add(stationStatistics[i].NODE_NAME);
                            item.SubItems.Add(stationStatistics[i].STATION_ID.ToString());
                            item.SubItems.Add(stationStatistics[i].ProductNum.ToString());
                            item.SubItems.Add(start.ToString());
                            item.SubItems.Add(end.ToString());

                            this.listViewStatistics.Items.Add(item);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void FormStatistics_Load(object sender, EventArgs e)
        {
            //修改默认的时长和开始时间
            this.comboBoxLong.SelectedIndex = 11;

            if (DateTime.Now.Hour < 9)
            {
                this.comboBoxStartTime.SelectedIndex = 20;
                this.dateTimePickerStartDate.Value = DateTime.Now.AddDays(-1).Date;
            }else if (DateTime.Now.Hour < 21)
            {
                this.comboBoxStartTime.SelectedIndex = 8;
                this.dateTimePickerStartDate.Value = DateTime.Now.Date;
            }
            else
            {
                this.comboBoxStartTime.SelectedIndex = 20;
                this.dateTimePickerStartDate.Value = DateTime.Now.Date;
            }

            NODE nodeW = new NODE();
            var selectNode = MethodWcf.SelectNodeClient(nodeW);

            if (selectNode.result)
            {
                _listNode = (from n in selectNode.value
                             where n.FLAG == true
                             select n).ToList();//排序后的工序名称
            }
            else
            {
                MessageBox.Show(selectNode.message);
            }
        }

        private void textBoxLineId_Enter(object sender, EventArgs e)
        {
            DialogLine line = new DialogLine(this.textBoxLineId);
            line.ShowDialog();
        }

        private void TextBoxNodeId_Enter(object sender, EventArgs e)
        {
            NODE nodeW = new NODE();
            DialogNode node = new DialogNode(this.textBoxNodeId, nodeW);
            node.ShowDialog();
        }
    }
}
